<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>
        var n = 5

        var countAndSay = function(n) {
            var str = '1'
            var newStr = ''
            while (--n){
                var s = 1
                for (let i=0;i<str.length;i++){
                    if (str[i]==str[i+1]){
                        s++
                    } else {
                        newStr+= s
                        newStr+= str.charAt(i)
                        s = 1
                    }
                }
                str = newStr
                newStr = ''
            }
            return str
        };
        console.log(countAndSay(n));

    </script>
</body>
</html>